home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr11 / mpc93jun.zip / ENVIRON.TXT < prev    next >
Text File  |  1993-04-19  |  16KB  |  364 lines

  1.                           The DOS Environment
  2.                             By Ken Johnson,
  3.  
  4.      The DOS environment is a special area of memory that DOS uses as
  5.      a "cubbyhole" to store information needed at a later time.
  6.      Knowing what it is and how it is used is becoming important, as
  7.      software programs and batch files get more sophisticated and make
  8.      more use of the environment.  This month we'll look at the
  9.      environment and environment variables.
  10.  
  11.      The DOS environment contains string (text) variables that DOS,
  12.      some application programs, and batch files can check and use.
  13.      Environment variables that DOS uses include PROMPT, PATH,
  14.      COMSPEC, and DIRCMD, which we'll discuss below.  Application
  15.      programs can create and use a variety of environment variables.
  16.      Three examples on my computers are TEMP (defines the Windows 3.0
  17.      directory for temporary files), GMK4 (defines the Grammatik IV
  18.      subdirectory), and 123CPBW (indicates whether Lotus 123 release
  19.      3.x is running on a monochrome monitor).
  20.  
  21.      The SET command is used to create and delete environment
  22.      variables.  Environment variables are "global", since once
  23.      created the can be used by any program or batch file (though they
  24.      can't be used on the command line).  Usually SET commands are
  25.      included in the AUTOEXEC.BAT file so that environment variables
  26.      will be created each time you boot up your computer.  Any
  27.      application program that uses environment variables will usually
  28.      add the SET statement to your AUTOEXEC.BAT file when it's
  29.      installed.
  30.  
  31.      The form of the SET command is:
  32.  
  33.      SET variable=value
  34.  
  35.      where "variable" is the variable name and "value" is the text
  36.      string it represents.  Some examples are:
  37.  
  38.      SET DIRCMD=/OE
  39.      SET TEMP=C:\WINDOWS\TEMP
  40.      SET COMSPEC=C:\DOS\COMMAND.COM
  41.      SET 123CPBW=YES
  42.  
  43.      The variable name is always converted to upper case, but the
  44.      value remains as it was entered.  Also be aware the names are
  45.      space sensitive, so "SET USER=JOE" is not the same as "SET USER =
  46.      JOE".  To remove an environment variable, enter SET variable=
  47.      with no value specified.
  48.  
  49.      By entering "SET" alone on the command line, you'll see a listing
  50.      of what's in the environment.  This will include your current
  51.      PATH and PROMPT, the name and path of your command interpreter
  52.      (i.e., COMMAND.COM) in the COMSPEC variable, and any other
  53.      environment variables that have been created.
  54.  
  55.      Now let's look at some environmental variables and how they are
  56.      used.
  57.  
  58.  
  59.      The COMSPEC Variable
  60.  
  61.      The COMSPEC environment variable points to the location (drive
  62.      and directory) of your command interpreter, usually COMMAND.COM.
  63.      This is important because COMMAND.COM loads into memory in two
  64.      parts, a permanent portion and a transient portion.  The
  65.      transient part of COMMAND.COM can be removed from RAM if an
  66.      application program needs that memory.  When the program ends,
  67.      DOS needs to reload COMMAND.COM to continue.
  68.  
  69.      The COMSPEC variable tells DOS where to find COMMAND.COM if it
  70.      needs to be reloaded.  If DOS can't find COMMAND.COM, you'll get
  71.      a "Cannot load COMMAND, system halted" message and have to
  72.      reboot.  Normally the COMSPEC variable is set at the beginning of
  73.      your AUTOEXEC.BAT file, but the preferred alternative is to use
  74.      the SHELL= statement in CONFIG.SYS to point to COMMAND.COM.
  75.      We'll discuss the SHELL statement below.
  76.  
  77.  
  78.      The PATH command/variable
  79.  
  80.      The PATH is a listing of the drives and directories that DOS
  81.      should search when you enter the name of an executable file on
  82.      the command line.  DOS will always look in the current (default)
  83.      directory first, then search the directories listed in the PATH
  84.      in the order specified.
  85.  
  86.      The PATH is specified as:
  87.  
  88.      PATH=[d:]\directory;[d:]\directory; . . .
  89.  
  90.      where "d:" is an option drive identifier and "directory" is the
  91.      directory name; you separate the directory names with a
  92.      semicolon.  The total length of the PATH statement must be less
  93.      than 128 characters, since the DOS command line is limited to 127
  94.      characters.  Notice that though PATH is an environment variable,
  95.      you don't have to enter it with the SET command.  Both "SET PATH=
  96.      . . ." and "PATH= . . ." will work.
  97.  
  98.      Since the path is searched in the order specified, make sure to
  99.      put the directories containing your most used programs at the
  100.      beginning.  For example, PATH=C:\DOS;C:\;C:\UTILITY shows that
  101.      DOS will search for a program in this order:
  102.  
  103.      1.   The default directory (always searched first, before
  104.           the path is ever accessed)
  105.      2.   The DOS subdirectory on the C: drive
  106.      3.   The root directory of the C: drive (C:\)
  107.      4.   The UTILITY subdirectory on the C: drive
  108.  
  109.      Another key to the PATH is to keep it as short as possible; in
  110.      other words, don't put ALL your directories on the PATH.  There
  111.      are two reasons for this.  First, since the PATH is an
  112.      environment variable it takes up environment space that may be
  113.      needed by other variables.  In DOS 3.x for example, a 120
  114.      character PATH statement uses three-fourths of the default
  115.      environmental space.  Secondly, every time you make a typing
  116.      mistake DOS will search the entire path before returning the "Bad
  117.      command or file name" message.  Longer paths mean more time to
  118.      wait each time you make a mistake.
  119.  
  120.      Since the PATH is part of the environment, you can view the path
  121.      with the other environment variables by entering "SET" at the DOS
  122.      prompt.  You also can enter "PATH" or "PATH=" to see just the
  123.      currently defined PATH.  To remove the PATH from the environment
  124.      (thus forcing DOS only to search the current directory), enter
  125.      either "PATH ;" or "SET PATH=" at the DOS prompt.
  126.  
  127.  
  128.      The PROMPT command/variable
  129.  
  130.      You've seen the default DOS prompt hundreds of times:  the
  131.      infamous "C>".  The PROMPT variable determines what prompt is
  132.      displayed, so you can use it to change "C>" to something a little
  133.      more user friendly.  The prompt is specified as:
  134.  
  135.           PROMPT text
  136.  
  137.      where "text" is any string of text or special prompt characters.
  138.      These special characters will display system information as part
  139.      of the prompt:
  140.  
  141.      $P  --  the default directory
  142.      $T  --  the current time (HH:MM:SS.xx)
  143.      $D  --  the current date (MM-DD-YY)
  144.      $V  --  the DOS version
  145.      $N  --  the default drive
  146.      $_  --  Carriage Return plus Line Feed
  147.      $E  --  the ESCape character
  148.      $H  --  backspace (erases the previous character)
  149.      $G  --  the > character
  150.      $L  --  the < character
  151.      $B  --  the | character
  152.      $Q  --  the = character
  153.  
  154.      As you can see, the standard DOS prompt is $N$G, the default
  155.      drive and the ">" sign.  If you enter the PROMPT command with
  156.      nothing after it, DOS resets the prompt to this default.  As with
  157.      the PATH, you do not have to use the SET statement with PROMPT.
  158.      Some sample prompts:
  159.  
  160.      PROMPT $P$G
  161.      will display the current drive and directory with the
  162.      greater than sign.  This is the most common PROMPT setting, since
  163.      it shows you exactly where in the directory structure you are
  164.      located.  For example, if you are in the DOCS directory of the
  165.      WP51 directory on your C: drive, your prompt will show:
  166.  
  167.      C:\WP51\DOCS>_
  168.  
  169.      PROMPT Time is $T$H$H$H$H$H$H$_$P$_Command--$G
  170.      will give a multi level prompt (the six $H are used to erase
  171.      the seconds and hundredths from the time):
  172.  
  173.      Time is 10:35
  174.      C:\WP51\DOCS
  175.      Command-->_
  176.  
  177.      PROMPT
  178.      will reset the prompt to the default ($N$G):
  179.  
  180.      C:>
  181.  
  182.      There is one minor problem when having DOS display the current
  183.      directory ($P).  Each time you press Enter, DOS will read the
  184.      disk to find the correct directory for the prompt.  If your
  185.      default drive is a floppy drive and you take the disk out, DOS
  186.      will try to read the empty drive and you'll get an error message:
  187.  
  188.      Not ready reading drive A:
  189.      Abort, Retry, Fail?
  190.  
  191.      At this point you can put the disk back in and press R.  Or you
  192.      can simply press F, in which case your prompt will become:
  193.  
  194.      Current drive is no longer valid>_
  195.  
  196.      Just change back to your hard drive and your old prompt will
  197.      reappear.
  198.  
  199.  
  200.      The DIRCMD variable
  201.  
  202.      In DOS 5.0, the DIRectory command now supports several new
  203.      switches to control what files are displayed and in what order.
  204.      For example, the /OGEN switch will display files sorted by
  205.      extension and name, with directories listed first.  Rather than
  206.      have to remember (and type in) certain switches with every DIR
  207.      command, you can use the DIRCMD variable to indicate which
  208.      switches to be used by default.  For example,
  209.  
  210.      SET DIRCMD=/OGEN
  211.  
  212.      will give you a directory sorted by extension and name (with
  213.      directories first) every time you use the DIR command.
  214.  
  215.  
  216.      The TEMP variable
  217.  
  218.      The TEMP variable is used most often to tell a program where to
  219.      store temporary files.  These are "working" files created as the
  220.      program runs.  Under normal operation, the program will delete
  221.      these temporary files when  finished with them.  One such program
  222.      is Windows 3.0, which uses a \TEMP subdirectory to store it's
  223.      temporary files.  When you install Windows it will add the
  224.      appropriate SET TEMP statement to your AUTOEXEC.BAT file.
  225.      Assuming Windows is on your C: drive, this probably will be SET
  226.      TEMP=C:\WINDOWS\TEMP.
  227.  
  228.      One trick to speed up Windows is to use a RAM disk for the TEMP
  229.      subdirectory.  A RAM disk is an area of memory that is treated as
  230.      a physical disk drive.  You can set up a RAM disk by using a
  231.      driver such as RAMDRIVE.SYS, which comes with Windows.  Once
  232.      you've installed the RAM disk, change the SET TEMP statement in
  233.      your AUTOEXEC.BAT file to point the RAM disk's drive designator.
  234.      For example, if the RAM drive were drive E:, specify SET
  235.      TEMP=E:\.
  236.  
  237.      Since it is much faster to write to RAM than to a physical disk
  238.      drive, using a RAM disk for Windows' TEMP subdirectory will
  239.      improve Windows' performance.  Another benefit of using a RAM
  240.      disk for Windows' temporary files is that you'll be sure they are
  241.      deleted when no longer needed.  Windows normally deletes all
  242.      temporary files, unless it ends abnormally.  If you've every
  243.      gotten an "Unrecoverable Application Error" and had to reboot
  244.      your system, Windows may have left some temporary files in the
  245.      \TEMP subdirectory.  By using a RAM disk, the temporary files
  246.      will be wiped out whenever you reboot or turn off your system.
  247.  
  248.  
  249.      Using Environment Variables in Batch Files
  250.  
  251.      One powerful feature of environment variables is the ability to
  252.      use them in batch files in DOS 3.2 and above.  You surround the
  253.      name of the variable with percent signs (for example, %PATH%);
  254.      when the batch file executes, the variable name expands to it's
  255.      value.  Remember that the variable's value is case sensitive and
  256.      space sensitive.  For example, part of your batch file might
  257.      include:
  258.  
  259.      [==> SHARON, please use smaller font so that lines are not split
  260.      <==]
  261.  
  262.      SET DONE=false
  263.      :LOOP
  264.        .
  265.        .  (other commands here, one of which is SET DONE=true)
  266.        .
  267.      IF %DONE%==false GOTO LOOP
  268.  
  269.      Since the path is an environment variable, it is often used in
  270.      batch files.  For example, you can create two simple batch files
  271.      that will add a directory to your current path in the first
  272.      position, then restore the original path.  These are NEWPATH.BAT
  273.      and OLDPATH.BAT:
  274.  
  275.      [==> Note: please use smaller font so that lines are not split
  276.      <==]
  277.  
  278.      ECHO OFF
  279.      REM NEWPATH.BAT
  280.      IF %1!==! GOTO NODIR
  281.      SET OLD=%PATH%
  282.      PATH=%1;%OLD%
  283.      ECHO Your path is now %PATH%
  284.      GOTO END
  285.      :NODIR
  286.      ECHO Please enter the directory to add to the path
  287.      :END
  288.  
  289.  
  290.      ECHO OFF
  291.      REM OLDPATH.BAT
  292.      IF %OLD%!==! GOTO END
  293.      PATH=%OLD%
  294.      SET OLD=
  295.      :END
  296.  
  297.      The NEWPATH batch file first checks to make sure that a new
  298.      directory was entered on the command line (which becomes
  299.      replaceable parameter %1).  It then stores the original path in a
  300.      new environment variable called OLD.  The PATH is redefined, with
  301.      the new directory appended on the front of the old path
  302.      (%1;%OLD%).  The batch file will then display the new path with
  303.      the ECHO command.
  304.  
  305.      When you are ready to restore the original path, simply execute
  306.      the OLDPATH batch file.  It uses the OLD environment variable as
  307.      the new path, then deletes the OLD environmental variable.
  308.      Notice that the batch file first checks whether there is an OLD
  309.      environment variable, and simply branches to the end if it
  310.      doesn't exist.  Without an OLD environment variable the
  311.      "PATH=%OLD%" statement would resolve as "PATH=", which would
  312.      display the current path.
  313.  
  314.  
  315.      Expanding the Size of the Environment with SHELL
  316.  
  317.      Since it is an area of RAM, it is possible to fill the memory
  318.      allocated to the environment.  This is particularly true if you
  319.      have a long PATH and create several environment variables.  In
  320.      the NEWPATH batch file example above, you are significantly
  321.      increasing the size of your environment because it now contains
  322.      both the old path and the new path.  If the environment does
  323.      fill, you'll receive an "Out of Environment Space" message.  This
  324.      means you must increase the size of the environment.
  325.  
  326.      The default environment is 160 bytes in DOS 2.x, 3.x, and 4.x,
  327.      and 256 bytes in DOS 5.0.  In DOS 3.1 and above you can specify
  328.      the desired environment size with the SHELL command in your
  329.      CONFIG.SYS file.  In DOS 2.x and 3.0, you can only increase the
  330.      environment by modifying (also called "patching") the COMMAND.COM
  331.      program -- not a job for the faint hearted!
  332.  
  333.      The SHELL statement in CONFIG.SYS tells DOS name of the command
  334.      interpreter (usually COMMAND.COM), its location, and the size of
  335.      the DOS environment.  The SHELL statement usually looks something
  336.      like this:
  337.  
  338.      SHELL=C:\DOS\COMMAND.COM C:\DOS /P /E:512
  339.  
  340.      As you might guess, the "/E:nnn" switch sets the size of the
  341.      environment, 512 bytes in this example.  In DOS 3.2 and above,
  342.      simply specify the size in bytes.  If you are using DOS 3.1, the
  343.      environment is specified in 16-byte blocks, so indicate the
  344.      number of blocks on the /E switch.  To get a 512 byte
  345.      environment, include /E:32 on the SHELL command (16 bytes times
  346.      32 blocks = 512 bytes).
  347.  
  348.      Let's look at the other parts of the SHELL statement.
  349.      "C:\DOS\COMMAND.COM" shows the name and location of the command
  350.      interpreter.  "C:\DOS" points to the directory where COMMAND.COM
  351.      is located, so that it can be reloaded into memory if necessary.
  352.      This will create the COMSPEC variable in the environment, meaning
  353.      you don't have to explicitly SET it in your AUTOEXEC.BAT file.
  354.      Finally, the "/P" switch indicates COMMAND.COM should remain
  355.      permanently in memory.
  356.  
  357.      The DOS environment is one of those (perhaps few!) things with
  358.      computers that you may never need to worry about.  But making
  359.      good use of the environment by setting a custom PROMPT, and
  360.      efficient PATH, and a fast TEMP subdirectory can help you be a
  361.      more productive PC user.  Just keep an eye on your environment
  362.      size!
  363.  
  364.